<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use app\index\validate\Valid;

class Netguidequery extends Common
{

	//列表
    public function index()
    {

        $duojxs = $this->request->get('duojxs');
        $this->assign(array('duojxs'=>$duojxs));

        $where = [];
        $where['t.isdel'] = 0;
        $zhankai = $this->request->get('zhankai');
        $this->assign(array('zhankai'=>$zhankai));
        $sn = $this->request->get('sn');
        $this->assign(array('sn'=>$sn));
        if(trim($sn)){
            $where['t.sn'] = $sn;
        }

        $title = $this->request->get('title');
        $this->assign(array('title'=>$title));
        if(trim($title)){
            $where['t.title'] = ['like','%'.$title.'%'];
        }

        $zoning_head = $this->request->get('zoning_head');
        $this->assign(array('zoning_head'=>$zoning_head));
        if(trim($zoning_head)){
            $where['t.zoning'] = ['exp','in(select t.id from cg_market_zoning t join employee t2 on t2.id=t.head where t2.em_name like \'%'.$zoning_head.'%\')'];
        }

        $provice_head = $this->request->get('provice_head');
        $this->assign(array('provice_head'=>$provice_head));
        if(trim($provice_head)){
            $where['t.provice'] = ['exp','in(select t.id from cg_market_provice t join employee t2 on t2.id=t.head where t2.em_name like \'%'.$provice_head.'%\')'];
        }

        $city_head = $this->request->get('city_head');
        $this->assign(array('city_head'=>$city_head));
        if(trim($city_head)){
            $where['t.city'] = ['exp','in(select t.id from cg_market_city t join employee t2 on t2.id=t.head where t2.em_name like \'%'.$city_head.'%\')'];
        }

        $city = $this->request->get('city');
        $this->assign(array('city'=>$city));
        if(trim($city)){
            $where['t.city'] = ['exp','in(select id from cg_market_city where title like \'%'.$city.'%\')'];
        }

        $zone = $this->request->get('zone');
        $this->assign(array('zone'=>$zone));
        if(trim($zone)){
            $where['t.zoning'] = ['exp','in(select id from cg_market_zoning where title like \'%'.$zone.'%\')'];
        }

        $provice = $this->request->get('provice');
        $this->assign(array('provice'=>$provice));
        if(trim($provice)){
            $where['t.provice'] = ['exp','in(select id from cg_market_provice where title like \'%'.$provice.'%\')'];
        }


        $oneCType = $this->request->get('oneCType');
        $this->assign(array('oneCType'=>$oneCType));
        if(trim($oneCType)){
            $where['t.system_id'] = ['exp','in(select t.id from cg_system t join cg_channel_types1 t2 on t2.id=t.oneCType where t2.title like \'%'.$oneCType.'%\')'];
        }

        $twoCType = $this->request->get('twoCType');
        $this->assign(array('twoCType'=>$twoCType));
        if(trim($twoCType)){
            $where['t.system_id'] = ['exp','in(select t.id from cg_system t join cg_channel_types2 t2 on t2.id=t.twoCType where t2.title like \'%'.$twoCType.'%\')'];
        }

        $cate = $this->request->get('cate');
        $this->assign(array('cate'=>$cate));
        if(trim($cate)){
            $where['t.cat'] = $cate;
        }

        $iskey = $this->request->get('iskey');
        $this->assign(array('iskey'=>$iskey));
        if(''!=$iskey){
            $where['t.iskey'] = $iskey;
        }

        $guide = $this->request->get('guide');
        $this->assign(array('guide'=>$guide));
        if(''!=trim($guide)){
            $where['t.guide'] = $guide;
        }

        $sample = $this->request->get('sample');
        $this->assign(array('sample'=>$sample));
        if(''!=trim($sample)){
            $where['t.sample'] = $sample;
        }
        $direct = $this->request->get('direct');
        $this->assign(array('direct'=>$direct));
        if(''!=trim($direct)){
            $where['t.direct'] = $direct;
        }

        $grade_s = $this->request->get('grade');
        $this->assign(array('grade_s'=>$grade_s));
        if(trim($grade_s)){
            $where['t.grade'] = $grade_s;
        }

        $jxs_sn = $this->request->get('jxs_sn');
        $this->assign(array('jxs_sn'=>$jxs_sn));
        if(trim($jxs_sn)){
            $where['t.system_id'] = ['exp',"in(select system_id from cg_system_jxs tt join cg_jxs tt2 on tt2.id=tt.jxs_id where tt2.sn like '%{$jxs_sn}%' )"];
        }

        $jxs_title = $this->request->get('jxs_title');
        $this->assign(array('jxs_title'=>$jxs_title));
        if(trim($jxs_title)){

            if(trim($duojxs)&&$duojxs==1){ //一行经销商

                $jxs_ids = Db::table('cg_jxs')->where(['title'=>['like','%'.$jxs_title.'%']])->field('id')->select();
                foreach($jxs_ids as $k=>$v){
                    $jxs_ids[$k] = $v['id'];
                }
                $jxs_ids = implode(',',$jxs_ids);
                $system_ids = Db::table('cg_system_jxs')->where(['jxs_id'=>['in',$jxs_ids]])->field('system_id')->select();
                foreach($system_ids as $k=>$v){
                    $system_ids[$k] = $v['system_id'];
                }
                $system_ids = implode(',',$system_ids);
                $where['t.system_id'] = ['in',$system_ids];

            }else{  //默认多行经销商
                $where['t3.jxs_id'] = ['exp',"in(select id from cg_jxs where title like '%{$jxs_title}%')"];
            }

        }

        $jxs_zoning = $this->request->get('jxs_zoning');
        $this->assign(array('jxs_zoning'=>$jxs_zoning));
        if($jxs_zoning){
            if(trim($duojxs)&&$duojxs==1){ //一行经销商

                $jxs_ids = Db::table('cg_market_zoning')->where(['title'=>['like','%'.$jxs_zoning.'%']])->field('id')->select();
                foreach($jxs_ids as $k=>$v){
                    $jxs_ids[$k] = $v['id'];
                }
                $jxs_ids = implode(',',$jxs_ids);
                $jxs_ids = Db::table('cg_jxs')->where(['zoning'=>['in',$jxs_ids]])->field('id')->select();
                foreach($jxs_ids as $k=>$v){
                    $jxs_ids[$k] = $v['id'];
                }
                $system_ids = Db::table('cg_system_jxs')->where(['jxs_id'=>['in',$jxs_ids]])->field('system_id')->select();
                foreach($system_ids as $k=>$v){
                    $system_ids[$k] = $v['id'];
                }
                $system_ids = implode(',',$system_ids);
                $where['system_id'] = ['in',$system_ids];

            }else{  //默认多行经销商
                $where['t3.jxs_id'] = ['exp',"in(select t.id from cg_jxs t join cg_market_zoning t2 on t2.id=t.zoning where t2.title like '%{$jxs_title}%')"];
            }

        }
        $jxs_provice = $this->request->get('jxs_provice');
        $this->assign(array('jxs_provice'=>$jxs_provice));
        if($jxs_provice){
            if(trim($duojxs)&&$duojxs==1){ //一行经销商

                $jxs_ids = Db::table('cg_market_provice')->where(['title'=>['like','%'.$jxs_provice.'%']])->field('id')->select();
                foreach($jxs_ids as $k=>$v){
                    $jxs_ids[$k] = $v['id'];
                }
                $jxs_ids = implode(',',$jxs_ids);
                $jxs_ids = Db::table('cg_jxs')->where(['provice'=>['in',$jxs_ids]])->field('id')->select();
                foreach($jxs_ids as $k=>$v){
                    $jxs_ids[$k] = $v['id'];
                }
                $system_ids = Db::table('cg_system_jxs')->where(['jxs_id'=>['in',$jxs_ids]])->field('system_id')->select();
                foreach($system_ids as $k=>$v){
                    $system_ids[$k] = $v['id'];
                }
                $system_ids = implode(',',$system_ids);
                $where['system_id'] = ['in',$system_ids];

            }else{  //默认多行经销商
                $where['t3.jxs_id'] = ['exp',"in(select t.id from cg_jxs t join cg_market_provice t2 on t2.id=t.provice where t2.title like '%{$jxs_provice}%')"];
            }

        }
        $jxs_city = $this->request->get('jxs_city');
        $this->assign(array('jxs_city'=>$jxs_city));
        if($jxs_city){
            if(trim($duojxs)&&$duojxs==1){ //一行经销商

                $jxs_ids = Db::table('cg_market_city')->where(['title'=>['like','%'.$jxs_city.'%']])->field('id')->select();
                foreach($jxs_ids as $k=>$v){
                    $jxs_ids[$k] = $v['id'];
                }
                $jxs_ids = implode(',',$jxs_ids);
                $jxs_ids = Db::table('cg_jxs')->where(['city'=>['in',$jxs_ids]])->field('id')->select();
                foreach($jxs_ids as $k=>$v){
                    $jxs_ids[$k] = $v['id'];
                }
                $system_ids = Db::table('cg_system_jxs')->where(['jxs_id'=>['in',$jxs_ids]])->field('system_id')->select();
                foreach($system_ids as $k=>$v){
                    $system_ids[$k] = $v['id'];
                }
                $system_ids = implode(',',$system_ids);
                $where['system_id'] = ['in',$system_ids];

            }else{  //默认多行经销商
                $where['t3.jxs_id'] = ['exp',"in(select t.id from cg_jxs t join cg_market_city t2 on t2.id=t.city where t2.title like '%{$jxs_city}%')"];
            }

        }
        $system_sn = $this->request->get('system_sn');
        $this->assign(array('system_sn'=>$system_sn));
        if($system_sn){
            $where['t.system_id'] = ['exp','in(select id from cg_system where sn like \''.$system_sn.'\')'];
        }
        $system_title = $this->request->get('system_title');
        $this->assign(array('system_title'=>$system_title));
        if($system_title){
            $where['t.system_id'] = ['exp','in(select id from cg_system where title like \''.$system_title.'\')'];
        }

        $stuff = $this->request->get('stuff');
        $this->assign(array('stuff'=>$stuff));
        if($stuff){
            //根据模糊查询规则把admin对应的id全部查询出来
            $ids = Db::query("select id from employee where em_name like '%{$stuff}%'");
            $str = [];
            foreach($ids as $k=>$v){
                if($k==0)
                    $str[] = " like '%\"$v[id]\"%' ";
                else
                    $str[] = " t.stuff like '%\"$v[id]\"%' ";
            }
            $str = implode('or',$str);

            $where['t.stuff'] = array('exp',$str);
        }


        //地址经纬度
        $address = $this->request->get('address');
        $this->assign(array('address'=>$address));
        if($address){
            switch ($address){
                case 1:
                    $where['t.lat'] = ['exp','is not null and t.lat <> \'\''];
                    $where['t.lng'] = ['exp','is not null and t.lng <> \'\''];
                    $where['t.detailAddress'] = ['exp','is null or t.detailAddress=\'\''];
                    break;
                case 2:
                    $where['t.lat'] = ['exp','is null or t.lat=\'\''];
                    $where['t.lng'] = ['exp','is null or t.lng=\'\''];
                    $where['t.detailAddress'] = ['exp','is not null and t.detailAddress<>\'\''];
                    break;
                case 3:
                    $where['t.lat'] = ['exp','is null or t.lat=\'\''];
                    $where['t.lng'] = ['exp','is null or t.lng=\'\''];
                    $where['t.detailAddress'] = ['exp','is null or t.detailAddress=\'\''];
                    break;
                default:
                    $where['t.lat'] = ['exp','is not null and t.lat <> \'\''];
                    $where['t.lng'] = ['exp','is not null and t.lng <> \'\''];
                    $where['t.detailAddress'] = ['exp','is not null and t.detailAddress<>\'\''];
            }
        }


        if(trim($duojxs)&&$duojxs==1){
            $count = Db::table('cg_net t')->where($where)->count();
        }else{
            $count = Db::table('cg_net t')->join('cg_system t2','t2.id=t.system_id')->join('cg_system_jxs t3','t3.system_id=t2.id')->where($where)->count();
        }

        if(trim($duojxs)&&$duojxs==1){
            $data = Db::table('cg_net t')->order("t.id desc")->where($where)->paginate(10,$count);
        }else{
            $data = Db::table('cg_net t')->field('t.*,t3.jxs_id')->order('t.id desc')->join('cg_system t2','t2.id=t.system_id')->join('cg_system_jxs t3','t3.system_id=t2.id')->where($where)->paginate(10,$count);
        }
//        echo Db::table('cg_net')->getLastSql();
        $page = $data->render();
        $data = $data->items();
        $maxpage=ceil($count/10);
        foreach($data as $k=>$v){
            $data[$k]['cusername'] = Db::table('users')->where(array('id'=>$v['cuser']))->value('login_name');
            $data[$k]['pusername'] = Db::table('users')->where(array('id'=>$v['puser']))->value('login_name');
            $data[$k]['ctime'] = date('Y-m-d H:i',$v['ctime']);
            $data[$k]['ptime'] = date('Y-m-d H:i',$v['ptime']);
            $data[$k]['zoning'] = Db::table('cg_market_zoning')->where('id',$v['zoning'])->value('title');
            $data[$k]['provice'] = Db::table('cg_market_provice')->where('id',$v['provice'])->value('title');
            $data[$k]['city'] = Db::table('cg_market_city')->where('id',$v['city'])->value('title');
            if($v['stuff']){
                $temp = unserialize($v['stuff']);
                $temp = implode(',',$temp);
                $temp = Db::query("select em_name from employee where id in(".$temp.")");
                $temp2 = [];
                foreach($temp as $v1){
                    $temp2[] = $v1['em_name'];
                }
                $data[$k]['stuff'] = implode(',',$temp2);
            }

            $data[$k]['system_sn'] = Db::table('cg_system')->where('id',$v['system_id'])->value('sn');
            $data[$k]['system_title'] = Db::table('cg_system')->where('id',$v['system_id'])->value('title');

            //所属经销商编号
            if(trim($duojxs)&&$duojxs==1){
                $temp = Db::table('cg_system_jxs t')->join('cg_jxs t2','t.jxs_id=t2.id')->where('t.system_id',$v['system_id'])->field('t2.sn')->select();
                foreach($temp as $k1=>$v1){
                    $temp[$k1] = $v1['sn'];
                }
                $temp = implode('<hr>',$temp);
                $data[$k]['system_jxs_sn'] = $temp;

            }else{
                $data[$k]['system_jxs_sn'] = Db::table('cg_jxs')->where('id',$v['jxs_id'])->value('sn');

            }

            //所属经销商名称
            if(trim($duojxs)&&$duojxs==1){
                $temp = Db::table('cg_system_jxs t')->join('cg_jxs t2','t.jxs_id=t2.id')->where('t.system_id',$v['system_id'])->field('t2.title')->select();
                foreach($temp as $k1=>$v1){
                    $temp[$k1] = $v1['title'];
                }
                $temp = implode('<hr>',$temp);
                $data[$k]['system_jxs_title'] = $temp;

            }else{
                $data[$k]['system_jxs_title'] = Db::table('cg_jxs')->where('id',$v['jxs_id'])->value('title');

            }

            //所属经销商大区
            if(trim($duojxs)&&$duojxs==1){
                $temp = Db::table('cg_system_jxs t')->join('cg_jxs t2','t.jxs_id=t2.id')->join('cg_market_zoning t3','t3.id=t2.zoning')->where('t.system_id',$v['system_id'])->field('t3.title')->select();
                foreach($temp as $k1=>$v1){
                    $temp[$k1] = $v1['title'];
                }
                $temp = implode('<hr>',$temp);
                $data[$k]['system_jxs_zoning'] = $temp;

            }else{
                $data[$k]['system_jxs_zoning'] = Db::table('cg_jxs t')->join('cg_market_zoning t2','t.zoning=t2.id')->where('t.id',$v['jxs_id'])->value('t2.title');

            }

            //所属经销商省份
            if(trim($duojxs)&&$duojxs==1){
                $temp = Db::table('cg_system_jxs t')->join('cg_jxs t2','t.jxs_id=t2.id')->join('cg_market_provice t3','t3.id=t2.provice')->where('t.system_id',$v['system_id'])->field('t3.title')->select();
                foreach($temp as $k1=>$v1){
                    $temp[$k1] = $v1['title'];
                }
                $temp = implode('<hr>',$temp);
                $data[$k]['system_jxs_provice'] = $temp;

            }else{
                $data[$k]['system_jxs_provice'] = Db::table('cg_jxs t')->join('cg_market_provice t2','t.provice=t2.id')->where('t.id',$v['jxs_id'])->value('t2.title');

            }

            //所属经销商城市
            if(trim($duojxs)&&$duojxs==1){
                $temp = Db::table('cg_system_jxs t')->join('cg_jxs t2','t.jxs_id=t2.id')->join('cg_market_city t3','t3.id=t2.city')->where('t.system_id',$v['system_id'])->field('t3.title')->select();
                foreach($temp as $k1=>$v1){
                    $temp[$k1] = $v1['title'];
                }
                $temp = implode('<hr>',$temp);
                $data[$k]['system_jxs_city'] = $temp;

            }else{
                $data[$k]['system_jxs_city'] = Db::table('cg_jxs t')->join('cg_market_city t2','t.city=t2.id')->where('t.id',$v['jxs_id'])->value('t2.title');

            }


            //分区负责人
            if(trim($duojxs)&&$duojxs==1){
                $temp = Db::table('cg_system_jxs t')->join('cg_jxs t2','t.jxs_id=t2.id')->join('cg_market_zoning t3','t3.id=t2.zoning')->join('employee t4','t4.id=t3.head')->where('t.system_id',$v['system_id'])->field('t4.em_name')->select();
                foreach($temp as $k1=>$v1){
                    $temp[$k1] = $v1['em_name'];
                }
                $temp = implode('<hr>',$temp);
                $data[$k]['system_jxs_zoning_head'] = $temp;

            }else{
                $data[$k]['system_jxs_zoning_head'] = Db::table('cg_jxs t')->join('cg_market_zoning t2','t.zoning=t2.id')->join('employee t3','t3.id=t2.head')->where('t.id',$v['jxs_id'])->value('t3.em_name');

            }
            //省份负责人
            if(trim($duojxs)&&$duojxs==1){

                $temp = Db::table('cg_system_jxs t')->join('cg_jxs t2','t.jxs_id=t2.id')->join('cg_market_provice t3','t3.id=t2.provice')->join('employee t4','t4.id=t3.head')->where('t.system_id',$v['system_id'])->field('t4.em_name')->select();
                foreach($temp as $k1=>$v1){
                    $temp[$k1] = $v1['em_name'];
                }
                $temp = implode('<hr>',$temp);
                $data[$k]['system_jxs_provice_head'] = $temp;

            }else{

                $data[$k]['system_jxs_provice_head'] = Db::table('cg_jxs t')->join('cg_market_provice t2','t.provice=t2.id')->join('employee t3','t3.id=t2.head')->where('t.id',$v['jxs_id'])->value('t3.em_name');

            }

            //城市负责人
            if(trim($duojxs)&&$duojxs==1){

                $temp = Db::table('cg_system_jxs t')->join('cg_jxs t2','t.jxs_id=t2.id')->join('cg_market_city t3','t3.id=t2.city')->join('employee t4','t4.id=t3.head')->where('t.system_id',$v['system_id'])->field('t4.em_name')->select();
                foreach($temp as $k1=>$v1){
                    $temp[$k1] = $v1['em_name'];
                }
                $temp = implode('<hr>',$temp);
                $data[$k]['system_jxs_city_head'] = $temp;

            }else{

                $data[$k]['system_jxs_city_head'] = Db::table('cg_jxs t')->join('cg_market_city t2','t.city=t2.id')->join('employee t3','t3.id=t2.head')->where('t.id',$v['jxs_id'])->value('t3.em_name');

            }

            //经销商业务人员
            if(trim($duojxs)&&$duojxs==1){

                $temp = Db::table('cg_system_jxs t')->join('cg_jxs t2','t.jxs_id=t2.id')->where('t.system_id',$v['system_id'])->field('t2.stuff')->select();

                foreach($temp as $k1=>$v1){
                    $t = unserialize($v1['stuff']);
                    $t = implode(',',$t);
                    $t = Db::query("select em_name from employee where id in(".$t.")");

                    foreach($t as $k2=>$v2){
                        $t[$k2] = $v2['em_name'];
                    }
                    $temp[$k1] =  implode(',',$t);
                }
                $temp = implode('<hr>',$temp);
                $data[$k]['system_jxs_stuff'] = $temp;

            }else{


                $temp = Db::table('cg_jxs')->where('id',$v['jxs_id'])->value('stuff');
                if($temp){
                    $temp = unserialize($temp);
                    $temp = implode(',',$temp);
                    $temp = Db::query("select em_name from employee where id in(".$temp.")");
                }else{
                    $temp=[];
                }
                
                $temp2 = [];
                foreach($temp as $v1){
                    $temp2[] = $v1['em_name'];
                }
                $data[$k]['system_jxs_stuff'] =  implode(',',$temp2);

            }


            $data[$k]['exec_provice'] = Db::table('cg_provice')->where('id',$v['exec_provice'])->value('title');
            $data[$k]['exec_city'] = Db::table('cg_city')->where('id',$v['exec_city'])->value('title');
            $data[$k]['exec_county'] = Db::table('cg_county')->where('id',$v['exec_county'])->value('title');
            $data[$k]['oneCType'] = Db::table('cg_channel_types1 t')->join('cg_system t2','t2.oneCType = t.id')->where('t2.id',$v['system_id'])->value('t.title');
            $data[$k]['twoCType'] = Db::table('cg_channel_types2 t')->join('cg_system t2','t2.twoCType = t.id')->where('t2.id',$v['system_id'])->value('t.title');
            $data[$k]['cat'] = Db::table('cg_networktype')->where('id',$v['cat'])->value('title');

            $data[$k]['jxs_title'] = Db::table('cg_jxs')->where('id',$v['jxs_id'])->value('title');
        }

        //查询网点分类
        $cat = Db::table('cg_networktype')->select();
        $this->assign('cat',$cat);

        //查询网点等级
        $grade = Db::table('cg_networklevel')->select();
        $this->assign('grade',$grade);

        $this->assign('data',$data);
        $this->assign('page',$page);
        $this->assign('maxpage',$maxpage);
        $this->assign('count',$count);
        return $this->fetch();
    }

    public function add(){

        if($_POST){

            $validate = new Valid([
                'sn'  => 'require|unique:cg_system_group',
                'title'  => 'require|unique:cg_system_group',
            ]);
            $data = [];
            $data['sn'] = $this->request->post('sn');
            $data['title'] = $this->request->post('title');
            $data['brief'] = $this->request->post('brief');

            if (!$validate->check($data)) {
                echo  json_encode([
                    "success"=>false,
                    "msg"=>$validate->getError()
                ]);die;
            }

            $data['cuser'] = $_SESSION['think']['employee_id'];
            $data['ctime'] = time();
            $data['puser'] = $_SESSION['think']['employee_id'];
            $data['ptime'] = time();

            $bool = Db::table('cg_system_group')->insert($data);
            if(false !== $bool){
                echo  json_encode([
                    "success"=>true,
                    "msg"=>"添加成功"
                ]);die;
            }else{
                echo  json_encode([
                    "success"=>false,
                    "msg"=>"添加失败"
                ]);die;
            }

        }
        return $this->fetch('form');
    }

    /**
     * 修改
     * @return mixed
     * @throws \think\Exception
     */
    public function update(){
        $id = $this->request->param('id');

        if($_POST){
            $validate = new Valid([
                'sn'  => 'require|unique:cg_system_group',
                'title'  => 'require|unique:cg_system_group',
            ]);
            $data = [];
            $data['id'] = $id;
            $data['sn'] = $this->request->post('sn');
            $data['title'] = $this->request->post('title');
            $data['brief'] = $this->request->post('brief');

            if (!$validate->check($data)) {
                echo  json_encode([
                    "success"=>false,
                    "msg"=>$validate->getError()
                ]);die;
            }

            $data['cuser'] = $_SESSION['think']['employee_id'];
            $data['ctime'] = time();
            $data['puser'] = $_SESSION['think']['employee_id'];
            $data['ptime'] = time();
            $bool = Db::table('cg_system_group')->where('id',$id)->update($data);
            if(false !== $bool){
                echo  json_encode([
                    "success"=>true,
                    "msg"=>"修改成功"
                ]);die;
            }else{
                echo  json_encode([
                    "success"=>false,
                    "msg"=>"修改失败"
                ]);die;
            }
            die;
        }

        $data = Db::table('cg_system_group')->where('id',$id)->find();


        $this->assign('data',$data);
        $this->assign('id',$id);

        return $this->fetch('form');
    }

    /**
     * 维护系统
     * @return mixed
     * @throws \think\Exception
     */
    public function whSystem(){
        $id = $this->request->param('id');

        $data = Db::table('cg_system_group')->where('id',$id)->find();

        //查询没分组的系统
        $meifenzu = Db::table('cg_system')->field('id,title')->where('groupid',0)->select();
        $yifenzu = Db::table('cg_system')->field('id,title')->where('groupid','<>',0)->select();


        $this->assign('data',$data);
        $this->assign('meifenzu',$meifenzu);
        $this->assign('yifenzu',$yifenzu);
        $this->assign('id',$id);

        return $this->fetch();
    }

    //    批量设置分组
    public function batchSet(){
        $id = $this->request->post('id');
        $pid = $this->request->post('pid');
        if(!$id||!$pid){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }
        $data = [];
        $data['groupid'] = $pid;
        $bool = Db::table('cg_system')->where('id','in',$id)->update($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"设置成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"设置失败"
            ]);die;
        }
    }

    //    批量取消设置分组
    public function batchSet2(){
        $id = $this->request->post('id');
        $pid = $this->request->post('pid');
        if(!$id||!$pid){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }
        $data = [];
        $data['groupid'] = 0;
        $bool = Db::table('cg_system')->where('id','in',$id)->update($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"设置成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"设置失败"
            ]);die;
        }
    }


    /**
     * 删除
     * @throws \think\Exception
     */
    public function delete(){
        $id = $this->request->param('id');
        if(!$id){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }
        $bool = Db::table('cg_system_group')->where(array('id'=>$id))->update(['isdel'=>1]);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"删除成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"删除失败"
            ]);die;
        }
    }


    /**
     * 启用
     * @throws \think\Exception
     */
    public function qiyong(){
        $id = $this->request->param('id');
        if(!$id){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }
        $bool = Db::table('cg_system_group')->where(array('id'=>$id))->update(['isdel'=>0]);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"删除成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"删除失败"
            ]);die;
        }
    }

    /**
     * 替换
     * @return mixed
     */
    public function tihuan(){
        $id = $this->request->param('id');
        if($_POST){;
            $new1 = $this->request->post('new1');
            $bool = Db::table('cg_system')->where('jxs',$id)->update(array('jxs'=>$new1));

            if(false!==$bool){
                $bool = Db::table('cg_system_jxs')->where(['jxs_id'=>$id,'system_id'=>$id])->update(array('jxs'=>$new1));

                echo  json_encode([
                    "success"=>true,
                    "msg"=>"替换成功"
                ]);
            }else{
                echo  json_encode([
                    "success"=>false,
                    "msg"=>"替换失败"
                ]);
            }
            die;
        }


        $this->assign('id',$id);
        $sn = Db::table('cg_jxs')->where('id',$id)->value('sn');
        $this->assign('sn',$sn);
        $title = Db::table('cg_jxs')->where('id',$id)->value('title');
        $this->assign('title',$title);
        $yiyou = Db::table('cg_system')->where('jxs',$id)->select();
        $this->assign('yiyou',$yiyou);

        return $this->fetch();
    }

    /**
     * 追加
     * @return mixed
     */
    public function zhuijia(){
        return $this->fetch();
    }

    //选择经销商
    public function selectJXS(){
        $this->assign('id',1);
        return $this->fetch();
    }
    //返回分区经销商树数据
    public function zonetreeajax(){
        $data = Db::table('cg_market_zoning')->where('isdel',0)->field('id,title')->select();//查分区
        $data2 = [];
        foreach($data as $k=>$v){
            $temp = [];
            $temp['name'] = $v['title'];
            //查询省份
            $t = Db::table('cg_market_provice')->where(array('zoning'=>$v['id']))->field('id,title')->select();//查省份
            $data3 = [];
            foreach($t as $kk=>$vv){
                $temp2['name'] = $vv['title'];
                //查城市
                $tt = Db::table('cg_market_city')->where(array('provice'=>$vv['id']))->field('id,title')->select();//查城市
                $data4 = [];
                foreach($tt as $k3=>$v3){
                    $temp3['name'] = $v3['title'];
                    //查城市
                    $t3 = Db::table('cg_jxs')->where(array('city'=>$v3['id']))->field('id,title name')->select();//查经销商
                    $temp3['children'] = $t3;
                    $temp3["chkDisabled"]=true;
                    $temp3["nocheck"]=true;
                    $data4[] = $temp3;
                }
                $temp2['children'] = $data4;
                $temp2["chkDisabled"]=true;
                $temp2["nocheck"]=true;
                $data3[] = $temp2;
            }
            $temp['children'] = $data3;
            $temp["chkDisabled"]=true;
            $temp["nocheck"]=true;
            $data2[] = $temp;
        }
        echo json_encode($data2);
    }

    /**
     * 查询经销商的部分信息
     */
    public function findJxs(){
        $id = $this->request->post('id');
        $data = Db::table('cg_jxs')->where('id',$id)->field('sn,zoning,provice,city')->find();
        $data['city'] = Db::table('cg_market_city t')->join('employee t2','t2.id= t.head')->where('t.id',$data['city'])->value('t2.em_name');
        $data['provice'] = Db::table('cg_market_provice t')->join('employee t2','t2.id= t.head')->where('t.id',$data['provice'])->value('t2.em_name');
        $data['zoning'] = Db::table('cg_market_zoning t')->join('employee t2','t2.id= t.head')->where('t.id',$data['zoning'])->value('t2.em_name');
        echo json_encode($data);die;
    }

    /**
     * 返回经销桑json数据
     */
    public function provicejson(){
        //查询所有省份
        $data = Db::table('pro_products')->field('pro_name title,id')->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }


    /**
     * 替换经销商
     * @return mixed
     * @throws \think\Exception
     */
    public function repDealer(){
        if($_POST){
            $data = [];
            $data['zoning'] = $this->request->post('zoning');
            $data['provice'] = $this->request->post('provice');
            $data['city'] = $this->request->post('city');
            $data['jxs'] = $this->request->post('jxs');
            $id = $this->request->param('id');

            $validate = new Valid([
                'zoning'  => 'require',
                'provice'  => 'require',
                'city'  => 'require',
                'jxs'  => 'require',
            ]);


            if (!$validate->check($data)) {
                echo  json_encode([
                    "success"=>false,
                    "msg"=>$validate->getError()
                ]);die;
            }

            $old = Db::table('cg_system')->where('id',$id)->value('jxs');
            $bool = Db::table('cg_system')->where('id',$id)->update($data);
            if(false !== $bool){
                Db::table('cg_system_jxs')->where(['system_id'=>$id,'jxs_id'=>$old,'default'=>1])->update(['jxs_id'=>$data['jxs']]);
                echo  json_encode([
                    "success"=>true,
                    "msg"=>"替换成功"
                ]);die;
            }else{
                echo  json_encode([
                    "success"=>false,
                    "msg"=>"替换失败"
                ]);die;
            }
        }
        $zoning = model('Zoning')->select();
        $this->assign('zoning',$zoning);
        return $this->fetch();
    }

    /**
     * 回收站
     * @return mixed
     */
    public function isdel()
    {

        $sn = $this->request->get('sn');
        $title = $this->request->get('title');
        $system_title = $this->request->get('systemName');

        $this->assign(array('sn'=>$sn));
        $this->assign(array('title'=>$title));
        $this->assign(array('systemName'=>$system_title));
        $where = [];
        $where['isdel'] = 1;
        if($sn){
            $where['sn'] = $sn;
        }
        if($title){
            $where['title'] = ['like','%'.$title.'%'];
        }
        if($system_title){
            $where['id'] = ['exp','in(select groupid from cg_system where title like \'%'.$system_title.'%\')'];
        }
//        if($stuff){
//            //根据模糊查询规则把admin对应的id全部查询出来
//            $ids = Db::query("select id from employee where em_name like '%{$stuff}%'");
//            $str = [];
//            foreach($ids as $k=>$v){
//                if($k==0)
//                    $str[] = " like '%\"$v[id]\"%' ";
//                else
//                    $str[] = " stuff like '%\"$v[id]\"%' ";
//            }
//            $str = implode('or',$str);
//
//            $where['stuff'] = array('exp',$str);
//        }

        $count = Db::table('cg_system_group')->where($where)->count();
        $data = Db::table('cg_system_group')->order("id desc")->where($where)->paginate(10,$count);
//        echo Db::table('cg_system')->getLastSql();
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $data[$k]['cusername'] = Db::table('users')->where(array('id'=>$v['cuser']))->value('login_name');
            $data[$k]['pusername'] = Db::table('users')->where(array('id'=>$v['puser']))->value('login_name');
            $data[$k]['ctime'] = date('Y-m-d H:i',$v['ctime']);
            $data[$k]['ptime'] = date('Y-m-d H:i',$v['ptime']);
        }

        $this->assign('data',$data);
        $this->assign('page',$page);
        return $this->fetch();
    }


    //维护分组货号
    public function whproductAR(){
        $id = $this->request->param('id');
        $data = Db::table('cg_system_group')->where('id',$id)->find();
        $data['xitongshu'] = Db::table('cg_system')->where('groupid',$id)->count();
        $data['danpinshu'] = Db::table('cg_system_group_product')->where('groupid',$id)->count();
        $this->assign('data',$data);

        //查询分组下的产品列表
        $list = Db::table('cg_system_group_product t')->join('pro_products t2','t2.id=t.pro_id')->field('t.*,t2.pro_no,t2.pro_name')->where(['groupid'=>$id])->select();
        $this->assign('list',$list);
        return $this->fetch();
    }

    //增加产品
    public function addProduct(){
        $id = $this->request->post('id');
        $groupid = $this->request->post('groupid');

        //查询是否已存在
        $count = Db::table('cg_system_group_product')->where(['pro_id'=>$id,'groupid'=>$groupid])->count();
        if($count<=0){
            $bool = Db::table('cg_system_group_product')->insert(['pro_id'=>$id,'groupid'=>$groupid]);
            if(false !== $bool) {
                echo json_encode(['success' => true, 'msg' => 'ok']);
            }else{
                echo json_encode(['success' => false,'msg'=>'添加失败']);
            }
        }else{
            echo json_encode(['success' => false,'msg'=>'该产品已存在']);
        }

    }

    //删除产品
    public function delProduct(){
        $ids = $this->request->post('ids');

        //查询是否已存在
        if($ids!=''){
            $bool = Db::table('cg_system_group_product')->where('id','in',$ids)->delete();
            if(false !== $bool) {
                echo json_encode(['success' => true, 'msg' => 'ok']);
            }else{
                echo json_encode(['success' => false,'msg'=>'删除失败']);
            }
        }else{
            echo json_encode(['success' => false,'msg'=>'参数错误']);
        }

    }

    //批量删除系统分组到回收站
    public function batchDelete(){
        $ids = $this->request->post('ids');

        //查询是否已存在
        if($ids!=''){
            $bool = Db::table('cg_system_group')->where('id','in',$ids)->update(['isdel'=>1]);
            if(false !== $bool) {
                echo json_encode(['success' => true, 'msg' => 'ok']);
            }else{
                echo json_encode(['success' => false,'msg'=>'删除失败']);
            }
        }else{
            echo json_encode(['success' => false,'msg'=>'参数错误']);
        }

    }

    //保存货号
    public function savesn(){

        $ids = $this->request->post('sn/a');

        //查询是否已存在
        if($ids!=''){
            foreach($ids as $k=>$v){
                Db::table('cg_system_group_product')->where('id',$k)->update(['sn'=>$v]);
            }
            $bool = Db::table('cg_system_group_product')->where('id','in',$ids)->delete();
            echo json_encode(['success' => true, 'msg' => 'ok']);
        }else{
            echo json_encode(['success' => false,'msg'=>'参数错误']);
        }

    }
}
